/*
 * @Author: hongbin
 * @Date: 2022-10-30 13:23:47
 * @LastEditors: hongbin
 * @LastEditTime: 2022-10-30 18:34:22
 * @Description: 不限制环境通用的three.js函数
 */
import * as THREE from "three";

/**
 * 随机颜色 继承自THREE的Color
 */
export class RandomColor extends THREE.Color {
    constructor() {
        super();
        this.r = Math.random();
        this.g = Math.random();
        this.b = Math.random();
    }
}

const defaultStyle = {
    "font-size": "10px",
    padding: "2px 4px",
    "border-radius": "3px",
    "letter-spacing": "1px",
};

const warnStyle = { ...defaultStyle, color: "#fff", background: "#afa" };
const logStyle = { ...defaultStyle, color: "#fff", background: "#51f" };
const errorStyle = { ...defaultStyle, color: "#fff", background: "#f00" };

const transitionStyle = (styleConf: Record<string, string>, mix?: Record<string, string>) => {
    let str = "";
    for (const [propName, value] of Object.entries(mix ? { ...styleConf, ...mix } : styleConf)) {
        str += `${propName}:${value};`;
    }
    return str;
};

/**
 * 花样打印信息 拒绝朴素的平庸的打印信息
 * 但只能打印基本数据类型 其他类型会被转换成字符串 调用 toString()方法
 */
export const PatternLog = {
    Warn: (msg: string | Number, style?: Record<string, string>): void => {
        console.log("%c%s", transitionStyle(warnStyle, style), `警告⚠️: ${msg}`);
    },
    Log: (msg: string | Number, style?: Record<string, string>): void => {
        console.log("%c%s", transitionStyle(logStyle, style), `${msg}`);
    },
    Error: (msg: string | Number, style?: Record<string, string>): void => {
        console.log("%c%s", transitionStyle(errorStyle, style), `错误🙅‍♂️: ${msg}`);
    },
};
